package com.anyou.mall.util;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * 腾讯云短信工具类 (订单发货)
 */
@Slf4j
public class SMSUtilOrderDeliver {
    /**
     * 应用的appid
     */
    public static int appid=1400642972;
    /**
     * 应用的appkey
     */
    public static String appkey="ed30ea2e5f05e4fdfc968402d541a8ab";
    /**
     *  短信的模版id
     */
    public static int templateId=1399195;
    /**
     * 短信的签名内容
     */
    public static String smsSign="江苏安邮信息技术";


    public static String sendSMS(String phoneNumber,String code) {
        String reStr = ""; //定义返回值

        try {
            //参数，一定要对应短信模板中的参数顺序和个数，code为验证码，5为验证码在redis中的存储时间为5分钟
            String[] params = {code};
            //创建ssender对象
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            //发送
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,templateId, params, smsSign, "", "");
            if(result.result!=0){
                log.error("发送短信失败，返回结果为：{}",result);
                reStr = "error";
            }
            reStr = "success";
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return reStr;

    }

    public static void main(String[] args) {
        sendSMS("17347126834","订单号");
    }
}
